/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Facade;

import entity.Account;
import entity.StatementAccount;
import java.sql.Date;
import java.sql.Time;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Vojtisek
 */
@MessageDriven(mappedName = "jms/Statement", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class StatementMDBBean implements MessageListener {

    private static final Logger logger = Logger.getLogger("Internet banking");
    @PersistenceContext
    private EntityManager em;

    public StatementMDBBean() {
    }

    public void onMessage(Message message) {
        try {
//            TextMessage tm = (TextMessage) message;
            MapMessage mm = (MapMessage) message;
            Integer accountId = mm.getInt("accountId");
            Long time = mm.getLong("time");
            Account account = em.find(Account.class, accountId);
            logger.log(Level.INFO, "StatementMDBBean  accountId: " + accountId);
            logger.log(Level.INFO, "StatementMDBBean  time: " + time);

            StatementAccount foo = new StatementAccount();
            foo.setAccount(account);
            foo.setDatum(new Date(time));
            foo.setCas(new Time(time));
            em.persist(foo);

//            logger.log(Level.INFO, "StatementMDBBean OK ID: " + tm.getText());
            logger.log(Level.INFO, "StatementMDBBean OK ID: " + account.getId());
        } catch (Exception e) {
            logger.log(Level.WARNING, "onMessage", e);
        }
    }
}  
